import pymysql
#定义全局变量
conn = None
cursor = None
try:
    #创建连接
    conn = pymysql.connect(host="127.3.0.28",port=8888,user="root",
                       password="admin123",database="test_db",charset="utf8")
    #获取游标
    cursor = conn.cursor()
    #执行sql语句
    cursor.execute("select * from test_book")
    #提取数据
    res = cursor.fetchone()
    print("查询结果为：",res)

except Exception as e:
    print("查询语句执行失败：",str(e))
finally:
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

